package cn.cocho.android.mc.async;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.os.Message;
import android.util.Log;
import android.util.Pair;

@Deprecated
class AsyncTask {

	private static AsyncHandler handler = new AsyncHandler();

	private static final ExecutorService executorService = Executors.newCachedThreadPool(new AsyncThreadFactory());

	public static void execute(Runnable job) {
		if (job == null) {
			return;
		}

		executorService.execute(job);
	}

	public static void async(final Runnable run) {
		if (run != null) {
			executorService.execute(run);
		}
	}

	public static void async(final ServiceAsync serviceAsync) {
		if (serviceAsync == null) {
			return;
		}

		execute(new Runnable() {

			@Override
			public void run() {
				Message msg = handler.obtainMessage();
				try {
					final Object data = serviceAsync.loadData();
					Pair<ServiceAsync, Object> pairData = new Pair<ServiceAsync, Object>(serviceAsync, data);
					msg.obj = pairData;
					handler.sendMessage(msg);
					return;
				} catch (Exception e) {
					Log.e(AsyncTask.class.getName(), "Exception happend when run async task.loadData()", e);
					Pair<ServiceAsync, Object> pairData = new Pair<ServiceAsync, Object>(serviceAsync, null);
					msg.obj = pairData;
					handler.sendMessage(msg);
				}
			}
		});
	}

}
